-- local URL = "http://www.wowhead.com/?quests=10.66&filter=si=1;cr=29:27:2;crs=2:2:1;crv=0:0:0" -- Zul'Drak
local URL = "http://www.wowhead.com/?quests=10.210&filter=maxrl=79;si=1;cr=29:27:2:11;crs=2:2:1:4;crv=0:0:0:1" -- Icecrown

local page
do
	local curl = require"luacurl"
	local temp = {}
	local c = curl.new()
	c:setopt(curl.OPT_URL, URL)
	c:setopt(curl.OPT_WRITEFUNCTION, function (t, s) t[#t + 1] = s return s:len() end)
	c:setopt(curl.OPT_WRITEDATA, temp)
	assert(c:perform())
	page = table.concat(temp)
	assert(page:len() > 0)
end

local result
for view in page:gmatch("new Listview(%b())") do
	local name = view:match("id: '([^']+)'")
	if name == 'quests' then
		result = {}
		local list = view:match("data%: (%b[])")
		for entry in list:gmatch("%b{}") do
			result[#result + 1] = tonumber(entry:match("{id:(%-?%d+)"))
		end
		break
	end
end
assert(result)
table.sort(result)

local ouput
do
	local f = io.open("check_zuldrak.lua", "w")
	output = function (p, ...)
		if string.find(p, "%", nil, true) and select('#', ...) > 0 then
			f:write(p:format(...))
		else
			f:write(p, ...)
		end
	end
end
output("-- %s\n", URL)
output("-- %d quests found\n", #result)
output("local quests = {%s}\n", table.concat(result, ','))
output([[

local f = CreateFrame"Frame"

f:SetScript("OnEvent", function (self)
	self:RegisterEvent"QUEST_QUERY_COMPLETE"
	self:UnregisterEvent"PLAYER_LOGIN"
	self:SetScript("OnEvent", function (self)
		local completed, incomplete = {}, {}
		GetQuestsCompleted(completed)
		for _, quest in quests do
			if not completed[quest] then
				incomplete[quest] = true
			end
		end
		print(("%d / %d incomplete quests in icecrown found"):format(#incomplete, #quests))
		print(("Quests: %s"):format(table.concat(incomplete, ','))
		self:UnregisterEvent"QUEST_QUERY_COMPLETE"
		self:SetScript("OnEvent", nil)
	end)
	QueryQuestsCompleted()
end)

f:RegisterEvent"PLAYER_LOGIN"
]])
